/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> vv;
        int treelevel=0;
        //vector<int> v;
        queue<TreeNode*> q;
        TreeNode* front=0;
        if(root)
        {
            treelevel=1;
            q.push(root);
        }
        while(!q.empty())
        {
            vector<int> v;
            for(int i=0;i<treelevel;++i)
            {
                front=q.front();
                q.pop();

                v.push_back(front->val);

                if(front->left)
                {
                    q.push(front->left);
                }

                if(front->right)
                {
                    q.push(front->right);
                }
            }
            vv.push_back(v);
            treelevel=q.size();
        }
        reverse(vv.begin(),vv.end());
        return vv;
    }
};
